﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Microsoft.Office.Interop.Word;
using System.Data.OracleClient;



public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        Microsoft.Office.Interop.Word._Application appWord = new Microsoft.Office.Interop.Word.ApplicationClass();
        Microsoft.Office.Interop.Word._Document docFile = null;
       
        try
        {
            appWord.Visible = false;
            object objTrue = true;
            object objFalse = false;

            //模板名称
            string DotName;
            DotName = "123.dot";

            object objTemplate = Server.MapPath(@"dot\\" + DotName);//模板路径
            object objDocType = Microsoft.Office.Interop.Word.WdDocumentType.wdTypeDocument;
            docFile = appWord.Documents.Add(ref objTemplate, ref objFalse, ref objDocType, ref objTrue);
            //第一步生成word文档
            //定义书签变量

            //临时用的传入变量，将来用实际参数替代
            string v_docbm, v_dwbm, v_nf, v_yf;
           
            v_docbm = "test";
            v_dwbm = "XB";
            v_nf = "2011";
            v_yf = "6";
           

            //查询该word用的取数配置信息
            string sql_query;
            sql_query = @"select * from zn_docjsxx a where a.docbm = '" + v_docbm
                + "' and (replace(replace(a.nf,\'[\',\'\'),\']\',\'\') = '" + v_nf
                + "' or a.nf is null) and (replace(replace(a.yf,\'[\',\'\'),\']\',\'\') = '" + v_yf 
                + "' or a.yf is null) order by a.jssx";

            //装入DataReader中
            OracleDataReader mydr = (OracleDataReader)Maticsoft.DBUtility.DbHelperOra.ExecuteReader(sql_query);

            //循环DataReader，调用过程取数返回数值           
            while (mydr.Read())
            {
                //书签插入的方式
                //object OSQ = mydr["SQMC"]; //书签名称
                //decimal v_sj;
                //v_sj = 0;

                //替换的字符的方式
                object Replace_String = mydr["SQMC"];       //要替换的字符 
                object ReplaceWith;             //最终替换成的字符
                object ms = System.Type.Missing;

                //计算信息ID
                Int32 JSXXID = Convert.ToInt32(mydr["DOCJSXXID"]);
                //计算描述
                string JSMS = mydr["JSMS"].ToString();

                //调用过程取出值               
                OracleParameter[] parameters ={     
                    new OracleParameter("p_docjsxxid",OracleType.Number,10),
                    new OracleParameter("p_jsms",OracleType.VarChar,100),
                    new OracleParameter("p_dwbm",OracleType.VarChar,100),
                    new OracleParameter("p_nf",OracleType.VarChar,100),
                    new OracleParameter("p_yf",OracleType.VarChar,100),
                    new OracleParameter("p_sj",OracleType.Number,10)
                    };

                parameters[0].Value = JSXXID;
                parameters[1].Value = JSMS;
                parameters[2].Value = v_dwbm;
                parameters[3].Value = v_nf;
                parameters[4].Value = v_yf;
                parameters[5].Direction = System.Data.ParameterDirection.Output;
                //parameters[5].Value = v_sj;

                //执行存储过程
               OracleDataReader odr =  Maticsoft.DBUtility.DbHelperOra.RunProcedure("p_doc_qs", parameters);
               odr.Close();

                try
                {
                    //v_sj = Convert.ToDecimal(parameters[5].Value);
                    ReplaceWith = parameters[5].Value.ToString();
                }
                catch
                {
                    //v_sj = 0;
                    ReplaceWith = null;
                }

                //给书签赋值---书签方式插入数值
                //docFile.Bookmarks.get_Item(ref OSQ).Range.Text = v_sj.ToString();

                //全文替换字符的方式---全文替换会丢格式
                //docFile.Content.Text = docFile.Content.Text.Replace("单击“添加”按钮","hahahaha"); ---全文替换会丢格式               
                
                //替换字符
                object Replace = Microsoft.Office.Interop.Word.WdReplace.wdReplaceAll;//设置替换方式:一，全部替换；二，只替换一个；三，一个都不替换。
                docFile.Content.Find.Execute(ref Replace_String, ref ms, ref ms, ref ms, ref ms, ref ms, ref ms, ref ms, ref ms, ref ReplaceWith, ref Replace, ref ms, ref ms, ref ms, ref ms);

            }
              

            //第四步 生成word
            DateTime dt = DateTime.Now;
            object filename = Server.MapPath("dot\\") + "表" + v_dwbm + dt.Ticks.ToString() + ".doc";
            object miss = System.Reflection.Missing.Value;
            docFile.SaveAs(ref filename, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss);
            object missingValue = Type.Missing;
            object doNotSaveChanges = Microsoft.Office.Interop.Word.WdSaveOptions.wdDoNotSaveChanges;
            docFile.Close(ref doNotSaveChanges, ref missingValue, ref missingValue);
            appWord.Quit(ref miss, ref miss, ref miss);
            docFile = null;
            appWord = null;

            //将文件名保存到数据库对应的单位、年份、月份、word编码中
            string sql_del_doc;
            sql_del_doc = "delete from zn_docfilename where nf = " + v_nf + " and yf = " + v_yf + " and dwbm = '" + v_dwbm + "' and docbm = '" + v_docbm + "'";
            Maticsoft.DBUtility.DbHelperOra.ExecuteSql(sql_del_doc);
            string sql_insert_doc;
            sql_insert_doc = "insert into zn_docfilename (NF,YF,DWBM,DOCBM,FILENAME) values (" + v_nf + "," + v_yf + ",'" + v_dwbm + "','" + v_docbm + "','" + filename + "')";
            Maticsoft.DBUtility.DbHelperOra.ExecuteSql(sql_insert_doc);
           


        }
        catch (Exception ex)
        {
            //捕捉异常，如果出现异常则清空实例，退出word,同时释放资源
            string aa = e.ToString();
            object miss = System.Reflection.Missing.Value;
            object missingValue = Type.Missing;
            object doNotSaveChanges = Microsoft.Office.Interop.Word.WdSaveOptions.wdDoNotSaveChanges;
            docFile.Close(ref doNotSaveChanges, ref missingValue, ref missingValue);
            appWord.Quit(ref miss, ref miss, ref miss);
            docFile = null;
            appWord = null;
            throw ex;
        }
    }


    protected void Button2_Click(object sender, EventArgs e)
    {
        //Response.Write("<script language=javascript>this.parent.rightFrame.location.href='D:\\西北统计系统\\Code\\WebApp\\Word\\dot\\表XB634454824373223104.doc'</script>");
        Response.Redirect("~/Word/dot/1.doc");　
    }
    
}
